package middle;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 给你一个由'1'（陆地）和 '0'（水）组成的的二维网格，请你计算网格中岛屿的数量。
 *
 * 岛屿总是被水包围，并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
 *
 * 此外，你可以假设该网格的四条边均被水包围。
 *
 * 链接：https://leetcode-cn.com/problems/number-of-islands
 * @author 胡宇轩
 * @Email: yuxuan.hu01@bianlifeng.com
 */
public class NumbersOfIsLands {
    /**
     * DFS
     * dfs识别一个岛屿
     * 外层的循环用来识别海洋中是否出现陆地，当出现了陆地，不过这个陆地的大小它总是为一个岛屿。
     * */
    class Solution {
        public int numIslands(char[][] grid) {
            int res = 0;
            for (int i = 0; i < grid.length; i++) {
                for (int j = 0; j < grid[0].length; j++) {
                    if(grid[i][j] == '1'){
                        res++;
                        dfs(grid,i,j);
                    }
                }
            }
            return res;
        }

        void dfs(char[][] grid, int rIndex, int cIndex){
            if(stopDfs(grid, rIndex, cIndex)){
                return;
            }
            // 把每一个遍历过的元素 改成0
            grid[rIndex][cIndex] = '0';
            dfs(grid, rIndex, cIndex+1);
            dfs(grid, rIndex+1, cIndex);
            dfs(grid, rIndex, cIndex-1);
            dfs(grid, rIndex-1, cIndex);
        }

        boolean stopDfs(char[][] grid, int rIndex, int cIndex){
           return (rIndex < 0 || rIndex >= grid.length) || (cIndex < 0 || cIndex >= grid[0].length) || grid[rIndex][cIndex] == '0';
        }
    }


    /**
     * BFS
     *
     * */
    class Solution1{
        public int numIslands(char[][] grid) {
            int landsCount = 0;
            int rLen = grid.length;
            int cLen = grid[0].length;
            for (int i = 0; i < rLen; i++) {
                for (int j = 0; j < cLen; j++) {
                    // 发现了新大陆
                    if(grid[i][j] == '1'){
                        ++landsCount;
                        grid[i][j] = '0';
                        Queue<Integer> queue = new LinkedList<>();
                        queue.add(i * cLen + j);
                        while (!queue.isEmpty()){
                            Integer curEle = queue.poll();
                            int r = curEle / cLen;
                            int c = curEle % cLen;
                            if(c + 1 < cLen && grid[r][c+1] == '1'){
                                queue.add(r * cLen + c+1);
                                grid[r][c+1] = '0';
                            }
                            if(c - 1 >= 0 && grid[r][c-1] == '1'){
                                queue.add(r * cLen + c-1);
                                grid[r][c-1] = '0';
                            }
                            if(r + 1 < rLen && grid[r+1][c] == '1'){
                                queue.add((r+1) * cLen + c);
                                grid[r+1][c] = '0';
                            }
                            if(r - 1 >= 0 && grid[r-1][c] == '1'){
                                queue.add((r-1) * cLen + c);
                                grid[r-1][c] = '0';
                            }
                        }
                    }

                }
            }
            return landsCount;
        }
    }
}
